package com.cqeec.demo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cqeec.demo.bean.AdminAuth;
import com.cqeec.demo.mapper.AdminAuthMapper;
import com.cqeec.demo.service.AdminAuthService;

/**
 * 系统用户菜单业务逻辑接口实现类
 * 
 * @Title: AdminAuthServiceImpl
 * @Descripton: 定义系统用户菜单业务逻辑接口实现
 */
@Service("adminAuthService")
@Transactional(propagation=Propagation.REQUIRED, rollbackFor={RuntimeException.class})
public class AdminAuthServiceImpl implements AdminAuthService {
	
	@Resource
	private AdminAuthMapper adminAuthMapper;

	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public List<AdminAuth> getUserMenu(List<String> ids) {
		return adminAuthMapper.getUserMenu(ids);
	}

	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public List<AdminAuth> getSysMenu() {
		return adminAuthMapper.getSysMenu();
	}
	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public boolean hasChildrens(Long menuId) {
		return adminAuthMapper.hasChildrens(menuId)> 0 ? true : false;
	}
	
	@Override
	@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
	public Map<String, Object> getMenuListByPage(Long id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		int counts = adminAuthMapper.recordCount(params);
		List<AdminAuth> auth_list =  adminAuthMapper.getMenuListByPage(params);
		Iterator<AdminAuth> it_auth = auth_list.iterator();
		List<AdminAuth> result_auth_list = new ArrayList<AdminAuth>();
		while(it_auth.hasNext())
		{
			AdminAuth auth = it_auth.next();
			Long pid = auth.getPid()==0 ? null : auth.getPid();
			auth.set_parentId(pid);
			String auth_type_name = auth.getAuth_type()==1 ? "页面导航菜单" : "页面按钮";
			auth.setAuth_type_name(auth_type_name);
			result_auth_list.add(auth);
		}
		Map<String, Object> result = new HashMap<>();
		result.put("status",true);
		result.put("message","");
		result.put("total", counts);
		result.put("rows", result_auth_list);
		return result;
	}
}
